

namespace Ex
{
    public class ObjectPath
    {
        private List<string> obj_node;

        public string FullPath
        {
            get {
                if (obj_node.Count == 0)
                {
                    return ".";
                }
                else
                {
                    return string.Concat(".", string.Join('.', obj_node));
                }
            }
        }

        public object? GetObject(IExTable table)
        {
            int idx = 0;
            for(; idx < obj_node.Count - 1; idx++)
            {
                var element = table.Get(obj_node[idx]);
                if (element is IExTable new_table)
                {
                    table = new_table;
                }
                else
                {
                    return null;
                }
            }
            return table.Get(obj_node[idx]);
        }

        public ObjectPath()
        {
            obj_node = new();
        }

        public ObjectPath AddNode(string node_name)
        {
            obj_node.Add(node_name);
            return this;
        }

        public static ObjectPath SetPath(string node_path)
        {
            ObjectPath result = new();
            result.obj_node.AddRange(node_path.Split('.', StringSplitOptions.RemoveEmptyEntries));
            return result;
        }

    }
}